package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private final String userID = "miguel";
    private final String password = "123456";   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 String user = request.getParameter("user");
	        String pwd = request.getParameter("pwd");
	         System.out.println(user);
	         System.out.println(pwd);
	        if(userID.equals(user) && password.equals(pwd)){
	            Cookie loginCookie = new Cookie("user",user);
	            //cookie  expira en 30 mins
	            loginCookie.setMaxAge(30*60);
	            response.addCookie(loginCookie);
	            response.sendRedirect("listadoCotizaciones.jsp");
	        }else{
	            RequestDispatcher rd = getServletContext().getRequestDispatcher("/fallo.jsp");
	            PrintWriter out= response.getWriter();
	            out.println("<font color=red>Su usuario o password no es correcto.</font>");
	            rd.include(request, response);
	        }
	}

}
